class Solution {
public:
    vector<int> countBits(int n) {
        vector<int> ans(n + 1);
        for (int i = 0; i <= n; i++) {
            int sum = 0;
            int j = i;
            while (j != 0) {
                sum++;
                j &= j - 1;
            }
            ans[i] = sum;
        }
        return ans;
    }
};